package cn.iocoder.yudao.module.system.framework.operatelog.core;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO;
import cn.iocoder.yudao.module.system.dal.mysql.dict.DictDataMapper;
import cn.iocoder.yudao.module.system.enums.DictTypeConstants;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mzt.logapi.service.IParseFunction;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class SupportingFacilitiesFunction implements IParseFunction {

    public static final String NAME = "getSupportingFacilities";

    @Resource
    DictDataMapper dictDataMapper;

    @Override
    public boolean executeBefore() {
        return true; // 先转换值后对比
    }

    @Override
    public String functionName() {
        return NAME;
    }

    @Override
    public String apply(Object value) {
        if (StrUtil.isEmptyIfStr(value)) {
            return "";
        }else {
            String[] split = value.toString().split(",");
            List<DictDataDO> dictDataDOS = dictDataMapper.selectList(new LambdaQueryWrapper<DictDataDO>()
                    .eq(DictDataDO::getDictType,DictTypeConstants.YM_SUPPORTING_FACILITY));

            Map<String, String> collect = dictDataDOS.stream().collect(Collectors.toMap(DictDataDO::getValue, DictDataDO::getLabel));

            if (dictDataDOS == null) {
                return "";
            }

            String result = "";

            for (String s : split) {

                String string = collect.get(s);

                result = result + string + "|";

            }

            return result;
        }

    }
}
